package com.euphoria;

import org.json.JSONObject;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;

import com.euphoria.commands.RequestDetailsCommand;
import com.facebook.android.Example;
import com.facebook.android.IFacebookUIResponder;

public class MainActivity extends TabActivity implements IFacebookUIResponder {

	private static final int REQUEST_CODE = 10;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    Resources res = getResources(); // Resource object to get Drawables
	    if(res==null){
	    	Log.d("init", "resource is null DEEEEEEEEEEEEEEEPTHI");
	    }
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, com.euphoria.EHost.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("EHost").setIndicator("EHost",
	                      res.getDrawable(R.layout.ic_tab_ehost))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, com.euphoria.EOrganize.class);
	    spec = tabHost.newTabSpec("EOrganize").setIndicator("Facilitate",
	                      res.getDrawable(R.layout.ic_tab_eorganize))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, com.euphoria.ESettings.class);
	    spec = tabHost.newTabSpec("ESettings").setIndicator("Settings",
	                      res.getDrawable(R.layout.ic_tab_esettings))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTab(0);

        /*_birthdaybutton = (Button)findViewById(R.id.birthdaybutton);
        _birthdaybutton.setOnClickListener(new OnClickListener(){
        	public void onClick(View view) {
        		Intent myIntent = new Intent(view.getContext(), Birthday.class);
        		startActivityForResult(myIntent, 0);
        	}
        });*/

//
//       if (ACTIVE_EVENTS.length > 0) {
//        	//_hostButton.setVisibility(View.INVISIBLE);
//        	listView.setAdapter(new ArrayAdapter<String>(this, R.layout.listview, R.id.TextView01, ACTIVE_EVENTS));
//        } else if (listView != null) {
//        	listView.setVisibility(View.INVISIBLE);
//        }
//        Log.d("init", "Awesome.. its done");
        //Show Facebook login page first..
        Intent _fwdToFB = new Intent(this, Example.class);
        startActivityForResult(_fwdToFB, REQUEST_CODE);
    }


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			Toast.makeText(this, "Facebook login is successful", Toast.LENGTH_SHORT).show();
		}
	}


    public boolean handleResponse(JSONObject obj) {
    	return false;
    }

    @SuppressWarnings("unused")
	private void onHostEvent(View v) {
        Intent _openview = new Intent(this, View2.class);
        startActivity(_openview);

    	/*Example.mAsyncRunner.request("me", new RequestDetailsCommand(this));
    	Log.d("clickhere", "yo im here....");*/
    }

    private void onEventCreate() {
    	Bundle bundle = new Bundle();
    	bundle.putString("name", "My event from android");
		//bundle.putString("location", "somewhere");
    	bundle.putString("venue", "{street: South Main Street, city: Milpitas, state: California}");
		bundle.putString("start_time", "2011-03-08T10:13:00");
		bundle.putString("end_time", "2011-03-08T10:20:00");
		bundle.putString("privacy", "SECRET");
    	Example.mAsyncRunner.request("me/events", bundle, new RequestDetailsCommand(this));
    	Log.d("clickhere", "yo im here....");
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.hostEventButton:
        	onEventCreate();
            return true;
        case R.id.closeEvent:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}